package sbt;

import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import sbt.Login;
import sbt.model.Film;
import sbt.model.User;
public class SearchFilm extends sbt.pages.TestBase {
  
@BeforeClass
  public void testLogin() {
  	User user = new User().setLogin("admin").setPassword("admin");
  	app.getUserHelper().loginAs(user);
}

@BeforeMethod
  public void before() throws Exception {
	app.getFilmHelper().clearSearch();
}

public void AddFilm() throws Exception {
	String filmname = "Search Film";
	String filmyear = "2013";
	Film film = new Film().setTitle(filmname).setYear(filmyear );
	app.getFilmHelper().create(film);
	app.getNavigationHelper().openMainPage();
}

public void RemoveFilm() throws Exception {
	while(app.getFilmHelper().getPageElement("//div[@id='results']//div[starts-with(@alt,'Search2 Film')]").size() != 0){
		app.getFilmHelper().getPageElement("//div[@id='results']//div[starts-with(@alt,'Search2 Film')]").get(0).click();
		app.getFilmHelper().delete();
	}
}
 
@Test
  public void SearchFilmPass() throws Throwable {
	 	AddFilm();
	 	int arrSize = app.getFilmHelper().getPageElement("//div[@id='results']//div[starts-with(@alt,'Search Film')]").size();
		List<Film> filmList = app.getFilmHelper().search("Search Film");
		int searchSize = filmList.size();//app.getFilmHelper().getPageElement("//div[@id='results']//div[starts-with(@alt,'Search Film')]").size();
		assertTrue(arrSize == searchSize);
   }

 @Test
  public void SearchFilmEmpty() throws Throwable {
	RemoveFilm();
	List<Film> filmList = app.getFilmHelper().search("Search2 Film");
	String cont = app.getFilmHelper().getPageElement("//div[@id='content']//div[@class='content']").get(0).getText();
	assertTrue((filmList.size() == 0) && (cont.equals("No movies where found.")));
}

@AfterMethod
public void toHome() throws Exception {
	app.getNavigationHelper().openMainPage();
}
@AfterClass
public void testLogout(){
	app.getUserHelper().logout();}
}
